package service.impl;

import model.IUsers;
import model.Users;
import persistence.dao.DaoManager;
import persistence.dao.UsersDao;
import persistence.dao.base.ConnectionBehavior;
import persistence.dao.base.DaoCommand;
import persistence.entity.UsersEntity;
import service.UsersService;

public class UsersServiceImpl implements UsersService {

	private DaoManager daoManager;

	public UsersServiceImpl(DaoManager daoManager) {
		this.daoManager = daoManager;
	}

	protected DaoManager getDaoManager() {
		return daoManager;
	}

	@Override
	public IUsers getUsers(final String name, final String pass) {
		return (IUsers) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final UsersDao usersDao = manager.getDao(UsersDao.class);
				return convertToModel(usersDao.getUsers(name, pass));
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IUsers setUsers(final String pass) {
		return (IUsers) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final UsersDao usersDao = manager.getDao(UsersDao.class);
				usersDao.setUsers(pass);
				return null;
			}
		}, ConnectionBehavior.CLOSE);
	}

	private IUsers convertToModel(UsersEntity usersEntity) {
		IUsers users = null;
		if (usersEntity != null) {
			users = new Users(usersEntity.getId_user(),
					usersEntity.getName_user());
		}
		return users;
	}
}
